def merge(arr):
    # 终止条件
    if len(arr) > 1:
        # 取中间操作
        mid = len(arr) // 2
        left_half = arr[:mid]
        right_half = arr[mid:]
        merge(left_half)
        merge(right_half)

        # 合并操作
        k = i = j = 0
        while i < len(left_half) and j < len(right_half):
            if left_half[i] < right_half[j]:
                arr[k] = left_half[i]
                i += 1
            else:
                arr[k] = right_half[j]
                j += 1
            k += 1
        # 结束完之后，看还有没有
        while i < len(left_half):
            arr[k] = left_half[i]
            k += 1
            i += 1
        while j < len(right_half):
            arr[k] = right_half[j]
            k += 1
            j += 1


if __name__ == '__main__':
    array = [1, 23, 45, 21, 56, 14, 59, 6]
    merge(array)
    print(array)
